package com.wzl.policy.controller;

import com.wzl.common.DpPublicRequest;
import com.wzl.common.DpPublicResponse;
import com.wzl.common.DpUserEntity;
import com.wzl.common.constant.DpPublicConstants;
import com.wzl.common.errcode.DpErrorCodeEnum;
import com.wzl.policy.factory.FileUtilFactory;
import com.wzl.policy.service.DpfatoryService;
import com.wzl.policy.strategy.IPayment;
import com.wzl.policy.strategy.Order;
import com.wzl.policy.strategy.PayResult;
import com.wzl.policy.strategyNew.FileUtilStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @description:
 * @author: weizhongli
 * @create: 2022-08-27 15:40
 */
@Controller
public class DpfatoryController {

    @Autowired
    private DpfatoryService dpfatoryService;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private FileUtilFactory fileUtilFactory;



    @RequestMapping("test")
    @ResponseBody
    public DpPublicResponse testQuery(DpPublicRequest dpPublicRequest) {

        List<DpUserEntity> dpUserEntities = dpfatoryService.testQuery(dpPublicRequest);

        DpPublicResponse dpPublicResponse = new DpPublicResponse(DpPublicConstants.SUCCESS_CODE, DpPublicConstants.SUCCESS_MESSAGE, dpUserEntities);
        return dpPublicResponse;
    }

    /**
     * 支付接口
     * @param amount
     * @param paymentType
     * @return
     */
    @RequestMapping("pay/{amount}/{paymentType}")
    @ResponseBody
    public DpPublicResponse pay(@PathVariable("amount") int amount,
                                @PathVariable("paymentType") String paymentType) {
        Order order = new Order();
        order.setAmount(amount);
        order.setPaymentType(paymentType);

        // 根据支付类型获取对应的策略 bean
        IPayment payment = applicationContext.getBean(order.getPaymentType(), IPayment.class);

        // 开始支付
        PayResult payResult = payment.pay(order);

        return new DpPublicResponse(DpPublicConstants.SUCCESS_CODE, DpPublicConstants.SUCCESS_MESSAGE,payResult);
    }

    /**
     *  策略+工厂测试
     */
    @RequestMapping("strategyFactory/{type}")
    @ResponseBody
    public DpPublicResponse strategyFactory(@PathVariable("type") String type) {

        try {
            FileUtilStrategy strategy = fileUtilFactory.getStrategy(type);
            Boolean b = strategy.login("192.168.1.22", 21, "test", "123456");
            if (!b) {
                return new DpPublicResponse(DpPublicConstants.UNKNOWN_CODE, DpPublicConstants.FTP_LOGIN_ERROR);
            }
            return new DpPublicResponse(DpErrorCodeEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new DpPublicResponse(DpErrorCodeEnum.UNKNOWN_ERROR,e.toString());
        }

    }

}